package shaka.core.internal

import java.text.SimpleDateFormat
import java.util.Date

import shaka.core.params.DateDiff

/**
 * 日期運算物件(仿python)
 * 
 * @author Shaka
 *
 */
public class DateDiffImpl {
	private Map formatM = [:]

	/**
	 * 取得期期差值物件{@link DateDiff}
	 * @param margs
	 * @return
	 */
	public DateDiff dateDiff(Map margs){
		return new DateDiff(margs)
	}

	/**
	 * 日期相加
	 * @param date
	 * @param op
	 * @return
	 */
	public Date plus(Date date,DateDiff op){
		GregorianCalendar gre = new GregorianCalendar()
		gre.setTime(date)
		if(op.year != 0)
			gre.add(GregorianCalendar.YEAR, op.year)
		if(op.month != 0)
			gre.add(GregorianCalendar.MONTH, op.month)
		if(op.day != 0)
			gre.add(GregorianCalendar.DAY_OF_MONTH, op.day)
		if(op.hour != 0)
			gre.add(GregorianCalendar.HOUR, op.hour)
		if(op.min != 0)
			gre.add(GregorianCalendar.MINUTE, op.min)
		if(op.sec != 0)
			gre.add(GregorianCalendar.SECOND, op.sec)
		return new Date(gre.getTimeInMillis())
	}

	/**
	 * 日期相減
	 * @param date
	 * @param op
	 * @return
	 */
	public Date minus(Date date,DateDiff op){
		GregorianCalendar gre = new GregorianCalendar()
		gre.setTime(date)
		if(op.year != 0)
			gre.add(GregorianCalendar.YEAR, -op.year)
		if(op.month != 0)
			gre.add(GregorianCalendar.MONTH, -op.month)
		if(op.day != 0)
			gre.add(GregorianCalendar.DAY_OF_MONTH, -op.day)
		if(op.hour != 0)
			gre.add(GregorianCalendar.HOUR, -op.hour)
		if(op.min != 0)
			gre.add(GregorianCalendar.MINUTE, -op.min)
		if(op.sec != 0)
			gre.add(GregorianCalendar.SECOND, -op.sec)
		return new Date(gre.getTimeInMillis())
	}

	/**
	 * 計算日期差
	 * @param date1
	 * @param date2
	 * @return
	 */
	public DateDiff minus(Date date1,Date date2){
		DateDiff diff = new DateDiff()
		diff.year = year(date1) - year(date2)
		diff.month = month(date1) - month(date2)
		diff.day = day(date1) - day(date2)
		diff.hour = hour(date1) - hour(date2)
		diff.min = min(date1) - min(date2)
		diff.sec = sec(date1) - sec(date2)
		return diff
	}

	/**
	 * 取得年
	 * @param date
	 * @return
	 */
	public int year(Date date){
		Calendar c = Calendar.getInstance()
		c.setTime(date)
		return c.get(Calendar.YEAR)
	}

	/**
	 * 取得月
	 * @param date
	 * @return
	 */
	public int month(Date date){
		Calendar c = Calendar.getInstance()
		c.setTime(date)
		return c.get(Calendar.MONTH) + 1
	}

	/**
	 * 取得日
	 * @param date
	 * @return
	 */
	public int day(Date date){
		Calendar c = Calendar.getInstance()
		c.setTime(date)
		return c.get(Calendar.DAY_OF_MONTH)
	}

	/**
	 * 取得時
	 * @param date
	 * @return
	 */
	public int hour(Date date){
		Calendar c = Calendar.getInstance()
		c.setTime(date)
		return c.get(Calendar.HOUR_OF_DAY)
	}

	/**
	 * 取得分
	 * @param date
	 * @return
	 */
	public int min(Date date){
		Calendar c = Calendar.getInstance()
		c.setTime(date)
		return c.get(Calendar.MINUTE)
	}

	/**
	 * 取得秒
	 * @param date
	 * @return
	 */
	public int sec(Date date){
		Calendar c = Calendar.getInstance()
		c.setTime(date)
		return c.get(Calendar.SECOND)
	}

	/**
	 * 星期 (Calendar.SUNDAY ~ Calendar.SATURDAY)
	 * @param date
	 * @return
	 */
	public int dayOfWeek(Date date){
		Calendar c = Calendar.getInstance()
		c.setTime(date)
		return c.get(Calendar.DAY_OF_WEEK)
	}

	/**
	 * 取得日期格式
	 * @param pattern
	 * @return
	 */
	private SimpleDateFormat getFormat(String pattern){
		synchronized(formatM){
			SimpleDateFormat format = formatM.get(pattern)
			if(format == null){
				format = new SimpleDateFormat(pattern)
				formatM.put(pattern, format)
			}
			return format
		}
	}

	/**
	 * 日期格式轉換
	 * @param pattern
	 * @param date
	 * @return
	 */
	public Date dateParse(String pattern, String date){
		SimpleDateFormat format = getFormat(pattern)
		synchronized(format){
			return format.parse(date)
		}
	}

	/**
	 * 日期格式轉換
	 * @param pattern
	 * @param date
	 * @return
	 */
	public String dateFormat(String pattern, Date date){
		SimpleDateFormat format = getFormat(pattern)
		synchronized(format){
			return format.format(date)
		}
	}
}
